package Servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import controle.Controle;

import modelo.Contato;


public class ControleServelt extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Controle controle;
	
	public ControleServelt(){
		super();
		controle = new Controle();
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String nome = request.getParameter("nome");
		String recado = request.getParameter("recado");
	
		Contato contato = new Contato();
		
		contato.setNome(nome);
		contato.setRecado(recado);
		controle.inserir(contato);
		
		String jsp = "";
		try {
			ArrayList<Contato> listUsuario = Controle.listarTodos();
			if (listUsuario != null) {
				request.setAttribute("listUsuario", listUsuario);
				jsp = "/index.jsp";
			} else {
				String erro = "Nao existe registro!";
				request.setAttribute("erro", erro);
				jsp = "/erro.jsp";
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsp = "";
		}

		request.getRequestDispatcher(jsp).forward(request, response);
		
		
	}

}
